package cn.ecnu.vo;

import cn.ecnu.mapper.*;
import cn.ecnu.pojo.*;
import cn.ecnu.vo.toFront.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import static cn.ecnu.common.constant.BusinessConstant.*;

@Component
public class Converter {

    @Resource
    private RedisTemplate<Object, Object> redisTemplate;

    @Resource
    private UserMapper userMapper;

    @Resource
    private VisitorMapper visitorMapper;

    @Resource
    private SupervisorMapper supervisorMapper;

    @Resource
    private ConsultantMapper consultantMapper;

    @Resource
    private ConsultRecordMapper consultRecordMapper;

    @Resource
    private HelpRecordMapper helpRecordMapper;

    public VisitorVo getVisitorVo(Integer uid) {

        User user = userMapper.getUserById(uid);

        Visitor visitor = visitorMapper.getVisitorById(uid);

        VisitorVo vo = new VisitorVo(user, visitor);

        return vo;

    }

    public SupervisorVo getSupervisorVo(Integer uid) {

        User user = userMapper.getUserById(uid);

        Supervisor supervisor = supervisorMapper.getSupervisorById(uid);

        ValueOperations<Object, Object> opsForValue = redisTemplate.opsForValue();

        Integer timeToday = consultRecordMapper.getTimeToday(uid);
        if (timeToday == null) {
            timeToday = 0;
        }

        Integer countToday = consultRecordMapper.getCountToday(uid);
        if (countToday == null) {
            countToday = 0;
        }

        //督导只区分离线和在线
        String state = "";
        if (redisTemplate.hasKey(String.format(ALIVE_KEY, uid))) {
            state = "在线";
        } else {
            state = "离线";
        }

        Integer currentSession = (Integer) opsForValue.get(String.format(SUPERVISOR_ONGOING_COUNT_KEY, uid));
        if (currentSession == null) {
            currentSession = 0;
        }

        //通过督导 id 获取所有关联的咨询师
        List<Consultant> consultants = consultantMapper.getConsultantBySupervisorId(uid);
        //需要转换为 vo
        ArrayList<ConsultantVo> consultantVos = new ArrayList<>();
        for (Consultant consultant : consultants) {
            ConsultantVo consultantVo = getConsultantVo(consultant.getConsultantId());
            consultantVos.add(consultantVo);
        }

        ArrayList<Integer> defaultDuty = new ArrayList<>();
        for (int day = 1; day <= 7; day++) {
            Boolean hasDuty = redisTemplate.opsForSet().isMember(String.format(SUPERVISOR_DEFAULT_DUTY_KEY, day), uid);
            if (hasDuty) {
                defaultDuty.add(day);
            }
        }

        SupervisorVo vo = new SupervisorVo(user, supervisor, state, timeToday, countToday, currentSession, consultantVos, defaultDuty);

        return vo;

    }

    public ConsultantVo getConsultantVo(Integer uid) {

        User user = userMapper.getUserById(uid);

        Consultant consultant = consultantMapper.getConsultantById(uid);

        ValueOperations<Object, Object> opsForValue = redisTemplate.opsForValue();

        //咨询师有三种状态
        String state = "";
        if (redisTemplate.hasKey(String.format(ALIVE_KEY, uid))) {
            Long size = redisTemplate.opsForList().size(String.format(CONSULTANT_ONGOING_LIST_KEY, uid));
            if (size >= CONSULTANT_BUSY_THRESHOLD) {
                state = "忙碌";
            } else {
                state = "空闲";
            }
        } else {
            state = "离线";
        }

        Integer timeToday = consultRecordMapper.getTimeToday(uid);
        if (timeToday == null) {
            timeToday = 0;
        }

        Integer countToday = consultRecordMapper.getCountToday(uid);
        if (countToday == null) {
            countToday = 0;
        }

        Integer currentSession = (Integer) opsForValue.get(String.format(SUPERVISOR_ONGOING_COUNT_KEY, uid));
        if (currentSession == null) {
            currentSession = 0;
        }

        ConsultantVo vo = new ConsultantVo(user, consultant, state, countToday, timeToday, currentSession);

        return vo;
    }

    public ConsultRecordVo getConsultRecordVo(Integer recordId) {

        ConsultRecord record = consultRecordMapper.getConsultRecordById(recordId);

        ConsultantVo consultant = getConsultantVo(record.getToId());

        VisitorVo visitor = getVisitorVo(record.getFromId());

        Supervisor supervisor = supervisorMapper.getSupervisorByConsultRecordId(recordId);
        SupervisorVo supervisorVo = null;
        if (supervisor != null) {
            supervisorVo = getSupervisorVo(supervisor.getSupervisorId());
        }

        ConsultRecordVo vo = new ConsultRecordVo(record, consultant, visitor, supervisorVo);

        return vo;

    }

    public HelpRecordVo getHelpRecordVo(Integer recordId) {

        HelpRecord helpRecord = helpRecordMapper.getHelpRecordById(recordId);

        Integer consultantId = helpRecord.getFromId();

        ConsultantVo consultantVo = getConsultantVo(consultantId);

        HelpRecordVo vo = new HelpRecordVo(helpRecord, consultantVo);

        return vo;

    }

}













